 package server;


import com.alibaba.druid.pool.DruidPooledConnection;
import configs.ServerConfig;
import constants.JobConstants;
import database.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import tools.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

















 public class RankingWorker {
/*  20 */   private static final Logger log = LogManager.getLogger(RankingWorker.class.getName());
/*  21 */   private static final Map<Integer, List<RankingInformation>> rankings = new HashMap<>();
/*  22 */   private static final Map<String, Integer> jobCommands = new HashMap<>();
/*  23 */   private static final List<PokemonInformation> pokemon = new ArrayList<>();
/*  24 */   private static final List<PokedexInformation> pokemon_seen = new ArrayList<>();
/*  25 */   private static final List<PokebattleInformation> pokemon_ratio = new ArrayList<>();
/*  26 */   private static final List<Integer> itemSearch = new ArrayList<>();

   public static Integer getJobCommand(String job) {
/*  29 */     return jobCommands.get(job);
   }

   public static Map<String, Integer> getJobCommands() {
/*  33 */     return jobCommands;
   }

   public static List<RankingInformation> getRankingInfo(int job) {
/*  37 */     return rankings.get(Integer.valueOf(job));
   }

   public static List<PokemonInformation> getPokemonInfo() {
/*  41 */     return pokemon;
   }

   public static List<PokedexInformation> getPokemonCaught() {
/*  45 */     return pokemon_seen;
   }

   public static List<PokebattleInformation> getPokemonRatio() {
/*  49 */     return pokemon_ratio;
   }

   public static List<Integer> getItemSearch() {
/*  53 */     return itemSearch;
   }

   public static void start() {
/*  57 */     log.info("系统自动更新玩家排名功能已启动...");
/*  58 */     log.info("更新间隔时间为: " + ServerConfig.WORLD_REFRESHRANK + " 分钟1次。");
/*  59 */     Timer.WorldTimer.getInstance().register(() -> { jobCommands.clear(); rankings.clear(); pokemon.clear(); pokemon_seen.clear(); pokemon_ratio.clear(); itemSearch.clear(); updateRank(); },(60000 * ServerConfig.WORLD_REFRESHRANK));
   }









   public static void updateRank() {
/*  71 */     log.info("开始更新玩家排名...");
/*  72 */     long startTime = System.currentTimeMillis();
/*  73 */     loadJobCommands();
/*  74 */     DruidPooledConnection con = null;
     try {
/*  76 */       con = DatabaseConnection.getInstance().getConnection();
/*  77 */       con.setAutoCommit(false);
/*  78 */       updateRanking((Connection)con);
/*  79 */       updatePokemon((Connection)con);
/*  80 */       updatePokemonRatio((Connection)con);
/*  81 */       updatePokemonCaught((Connection)con);
/*  82 */       updateItemSearch((Connection)con);
/*  83 */       con.commit();
/*  84 */       con.setAutoCommit(true);
/*  85 */     } catch (Exception ex) {
       try {
/*  87 */         con.rollback();
/*  88 */         con.setAutoCommit(true);
/*  89 */         log.error("更新玩家排名出错", ex);
/*  90 */       } catch (SQLException ex2) {
/*  91 */         log.error("Could not rollback unfinished ranking transaction", ex2);
       }
     }
/*  94 */     log.info("玩家排名更新完成 耗时: " + ((System.currentTimeMillis() - startTime) / 1000L) + " 秒..");
   }

   private static void updateRanking(Connection con) throws Exception {
/*  98 */     String sb = "SELECT c.id, c.job, c.exp, c.level, c.name, c.jobRank, c.rank, c.fame FROM characters AS c LEFT JOIN accounts AS a ON c.accountid = a.id WHERE c.gm = 0 AND a.banned = 0 AND c.level >= 160 ORDER BY c.level DESC , c.exp DESC , c.fame DESC , c.rank ASC";


/* 101 */     PreparedStatement charSelect = con.prepareStatement(sb);
/* 102 */     ResultSet rs = charSelect.executeQuery();
/* 103 */     PreparedStatement ps = con.prepareStatement("UPDATE characters SET jobRank = ?, jobRankMove = ?, rank = ?, rankMove = ? WHERE id = ?");
/* 104 */     int rank = 0;
/* 105 */     Map<Integer, Integer> rankMap = new LinkedHashMap<>();
/* 106 */     for (Iterator<Integer> iterator = jobCommands.values().iterator(); iterator.hasNext(); ) { int i = ((Integer)iterator.next()).intValue();
/* 107 */       rankMap.put(Integer.valueOf(i), Integer.valueOf(0));
/* 108 */       rankings.put(Integer.valueOf(i), new ArrayList<>()); }

/* 110 */     while (rs.next()) {
/* 111 */       int job = rs.getInt("job");
/* 112 */       if (!rankMap.containsKey(Integer.valueOf(job / 100))) {
         continue;
       }
/* 115 */       int jobRank = ((Integer)rankMap.get(Integer.valueOf(job / 100))).intValue() + 1;
/* 116 */       rankMap.put(Integer.valueOf(job / 100), Integer.valueOf(jobRank));
/* 117 */       rank++;
/* 118 */       ((List<RankingInformation>)rankings.get(Integer.valueOf(-1))).add(new RankingInformation(rs.getString("name"), job, rs.getInt("level"), rs.getLong("exp"), rank, rs.getInt("fame")));
/* 119 */       ((List<RankingInformation>)rankings.get(Integer.valueOf(job / 100))).add(new RankingInformation(rs.getString("name"), job, rs.getInt("level"), rs.getLong("exp"), jobRank, rs.getInt("fame")));
/* 120 */       ps.setInt(1, jobRank);
/* 121 */       ps.setInt(2, rs.getInt("jobRank") - jobRank);
/* 122 */       ps.setInt(3, rank);
/* 123 */       ps.setInt(4, rs.getInt("rank") - rank);
/* 124 */       ps.setInt(5, rs.getInt("id"));
/* 125 */       ps.addBatch();
     }
/* 127 */     ps.executeBatch();
/* 128 */     rs.close();
/* 129 */     charSelect.close();
/* 130 */     ps.close();
   }

   private static void updatePokemon(Connection con) throws Exception {
/* 134 */     String sb = "SELECT count(distinct m.id) AS mc, c.name, c.totalWins, c.totalLosses  FROM characters AS c LEFT JOIN accounts AS a ON c.accountid = a.id RIGHT JOIN monsterbook AS m ON m.charid = a.id WHERE c.gm = 0 AND a.banned = 0 ORDER BY c.totalWins DESC, c.totalLosses DESC, mc DESC LIMIT 50";



/* 138 */     PreparedStatement charSelect = con.prepareStatement(sb);
/* 139 */     ResultSet rs = charSelect.executeQuery();
/* 140 */     int rank = 0;
/* 141 */     while (rs.next()) {
/* 142 */       rank++;
/* 143 */       pokemon.add(new PokemonInformation(rs.getString("name"), rs.getInt("totalWins"), rs.getInt("totalLosses"), rs.getInt("mc"), rank));
     }
/* 145 */     rs.close();
/* 146 */     charSelect.close();
   }

   private static void updatePokemonRatio(Connection con) throws Exception {
/* 150 */     String sb = "SELECT (c.totalWins / c.totalLosses) AS mc, c.name, c.totalWins, c.totalLosses  FROM characters AS c LEFT JOIN accounts AS a ON c.accountid = a.id WHERE c.gm = 0 AND a.banned = 0 AND c.totalWins > 10 AND c.totalLosses > 0 ORDER BY mc DESC, c.totalWins DESC, c.totalLosses ASC LIMIT 50";



/* 154 */     PreparedStatement charSelect = con.prepareStatement(sb);
/* 155 */     ResultSet rs = charSelect.executeQuery();
/* 156 */     int rank = 0;
/* 157 */     while (rs.next()) {
/* 158 */       rank++;
/* 159 */       pokemon_ratio.add(new PokebattleInformation(rs.getString("name"), rs.getInt("totalWins"), rs.getInt("totalLosses"), rs.getDouble("mc"), rank));
     }
/* 161 */     rs.close();
/* 162 */     charSelect.close();
   }

   private static void updatePokemonCaught(Connection con) throws Exception {
/* 166 */     String sb = "SELECT count(DISTINCT m.id) AS mc, c.name  FROM characters AS c LEFT JOIN accounts AS a ON c.accountid = a.id RIGHT JOIN monsterbook AS m ON m.charid = a.id WHERE c.gm = 0 AND a.banned = 0 ORDER BY mc DESC LIMIT 50";



/* 170 */     PreparedStatement charSelect = con.prepareStatement(sb);
/* 171 */     ResultSet rs = charSelect.executeQuery();
/* 172 */     int rank = 0;
/* 173 */     while (rs.next()) {
/* 174 */       rank++;
/* 175 */       pokemon_seen.add(new PokedexInformation(rs.getString("name"), rs.getInt("mc"), rank));
     }
/* 177 */     rs.close();
/* 178 */     charSelect.close();
   }

   private static void updateItemSearch(Connection con) throws Exception {
/* 182 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 183 */     PreparedStatement ps = con.prepareStatement("SELECT itemid, count FROM itemsearch WHERE count > 0 ORDER BY count DESC LIMIT 10");
/* 184 */     ResultSet rs = ps.executeQuery();
/* 185 */     itemSearch.clear();
/* 186 */     while (rs.next()) {
/* 187 */       int itemId = rs.getInt("itemid");



/* 191 */       if (itemSearch.contains(Integer.valueOf(itemId)) || !ii.itemExists(itemId)) {
         continue;
       }
/* 194 */       itemSearch.add(Integer.valueOf(itemId));
     }
/* 196 */     rs.close();
/* 197 */     ps.close();
   }

   public static void loadJobCommands() {
/* 201 */     jobCommands.put("所有", Integer.valueOf(-1));
/* 202 */     jobCommands.put("新手", Integer.valueOf(0));
/* 203 */     jobCommands.put("战士", Integer.valueOf(1));
/* 204 */     jobCommands.put("魔法师", Integer.valueOf(2));
/* 205 */     jobCommands.put("弓箭手", Integer.valueOf(3));
/* 206 */     jobCommands.put("飞侠", Integer.valueOf(4));
/* 207 */     jobCommands.put("海盗", Integer.valueOf(5));
/* 208 */     jobCommands.put("初心者", Integer.valueOf(10));
/* 209 */     jobCommands.put("魂骑士", Integer.valueOf(11));
/* 210 */     jobCommands.put("炎术士", Integer.valueOf(12));
/* 211 */     jobCommands.put("风灵使者", Integer.valueOf(13));
/* 212 */     jobCommands.put("夜行者", Integer.valueOf(14));
/* 213 */     jobCommands.put("奇袭者", Integer.valueOf(15));
/* 214 */     jobCommands.put("英雄", Integer.valueOf(20));
/* 215 */     jobCommands.put("战神", Integer.valueOf(21));
/* 216 */     jobCommands.put("龙神", Integer.valueOf(22));
/* 217 */     jobCommands.put("双弩精灵", Integer.valueOf(23));
/* 218 */     jobCommands.put("幻影神偷", Integer.valueOf(24));
/* 219 */     jobCommands.put("夜光法师", Integer.valueOf(27));
/* 220 */     jobCommands.put("反抗者", Integer.valueOf(30));
/* 221 */     jobCommands.put("恶魔猎手", Integer.valueOf(31));
/* 222 */     jobCommands.put("幻灵斗师", Integer.valueOf(32));
/* 223 */     jobCommands.put("弩豹游侠", Integer.valueOf(33));
/* 224 */     jobCommands.put("机械师", Integer.valueOf(35));
/* 225 */     jobCommands.put("米哈尔", Integer.valueOf(50));
   }

   public static class RankingInformation
   {
     public final String toString;
     public final int rank;

     public RankingInformation(String name, int job, int level, long exp, int rank, int fame) {
/* 234 */       this.rank = rank;






/* 241 */       String builder = "排名 " + StringUtil.getRightPaddedStr(String.valueOf(rank), ' ', 3) + " : " + StringUtil.getRightPaddedStr(name, ' ', 13) + " 等级: " + StringUtil.getRightPaddedStr(String.valueOf(level), ' ', 3) + " 职业: " + StringUtil.getRightPaddedStr(JobConstants.getJobNameById(job), ' ', 10) + "\r\n";





/* 247 */       this.toString = builder;
     }


     public String toString() {
/* 252 */       return this.toString;
     }
   }

   public static class PokemonInformation
   {
     public final String toString;

     public PokemonInformation(String name, int totalWins, int totalLosses, int caught, int rank) {
/* 261 */       String builder = "排名 " + rank + " : #e" + name + "#n - #r胜利: " + totalWins + "#b 失败: " + totalLosses + "#k Caught:" + caught + "\r\n";









/* 271 */       this.toString = builder;
     }


     public String toString() {
/* 276 */       return this.toString;
     }
   }

   public static class PokedexInformation
   {
     public final String toString;

     public PokedexInformation(String name, int caught, int rank) {
/* 285 */       String builder = "排名 " + rank + " : #e" + name + "#n - #rCaught: " + caught + "\r\n";





/* 291 */       this.toString = builder;
     }


     public String toString() {
/* 296 */       return this.toString;
     }
   }

   public static class PokebattleInformation
   {
     public final String toString;

     public PokebattleInformation(String name, int totalWins, int totalLosses, double caught, int rank) {
/* 305 */       String builder = "Rank " + rank + " : #e" + name + "#n - #rRatio: " + caught + "\r\n";





/* 311 */       this.toString = builder;
     }


     public String toString() {
/* 316 */       return this.toString;
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\server\RankingWorker.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */